home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / gfx / fract / FlashMandel.lha / Sources / Modules / ColorWindow.c next >
C/C++ Source or Header  |  2000-09-26  |  37KB  |  878 lines

  1. /*******************************************************************************
  2. **
  3. **  Coded by Dino Papararo                 11-Feb-1999
  4. **
  5. *******************************************************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <exec/execbase.h>
  9. #include <intuition/intuition.h>
  10. #include <intuition/gadgetclass.h>
  11. #include <proto/exec.h>
  12. #include <proto/intuition.h>
  13. #include <proto/graphics.h>
  14. #include <proto/gadtools.h>
  15. #include <proto/utility.h>
  16.  
  17. #include "compilerspecific.h"
  18.  
  19. #define MIN(a,b)    ((a)<(b)?(a):(b))
  20. #define MAX(a,b)    ((a)>(b)?(a):(b))
  21.  
  22. #define ACCEPT       (1L)
  23. #define RESET        (2L)
  24. #define CANCEL       (3L)
  25. #define RED          (4L)
  26. #define GREEN        (5L)
  27. #define BLUE         (6L)
  28. #define COPY         (7L)
  29. #define SWAP         (8L)
  30. #define SPREAD       (9L)
  31. #define UNDO         (10L)
  32. #define INVERT       (11L)
  33. #define PALETTE      (12L)
  34.  
  35. #define STARTPEN     (4L)
  36.  
  37. #define MINVALUE     (0L)
  38. #define MAXVALUE     (255L)
  39. #define DELTA        (10L)
  40.  
  41. BOOL ModifyPalette  (struct Window *,WORD,WORD,ULONG *);
  42. VOID KeepPalette    (struct Window *);
  43. VOID Copy           (struct Window *,ULONG);
  44. VOID Paste          (struct Window *,const ULONG);
  45. VOID Swap           (struct Window *,const ULONG,const ULONG);
  46. BOOL Spread         (struct Window *,const ULONG,const ULONG);
  47. VOID InvertPalette  (struct Window *,ULONG,ULONG);
  48.  
  49. IMPORT struct ExecBase *SysBase;
  50.  
  51. ULONG COLOR_RGB [3L * 256L + 2L],UNDO_RGB [3L * 256L + 2L],COPY_RGB [3L];
  52.  
  53. struct NewGadget BUTTON_GAD,SLIDER_GAD,PALETTE_GAD;
  54.  
  55. BOOL ModifyPalette (struct Window *Win,WORD LeftEdge,WORD TopEdge,ULONG *Palette32)
  56. {
  57. struct Window *ColorWin;
  58.  
  59. struct IntuiMessage *Message;
  60.  
  61. struct Gadget *GadList = NULL,*MyButtonGad,*MyPaletteGad;
  62.  
  63. struct Gadget *RedSliderGad,*GreenSliderGad,*BlueSliderGad,*MyGad;
  64.  
  65. BOOL Copy_Msg = FALSE,Swap_Msg = FALSE,Spread_Msg = FALSE,Exit = FALSE;
  66.  
  67. ULONG SelectedPen = STARTPEN,OldPen = NULL,NewPen;
  68.  
  69. UWORD MyCode;
  70.  
  71. ULONG MyClass,Colors,ColorBase = 3L * STARTPEN + 1L;
  72.  
  73. ULONG RedLevel,GreenLevel,BlueLevel;
  74.  
  75. CPTR *VInfo;
  76.  
  77.   Colors = 1L << Win->RPort->BitMap->Depth;
  78.  
  79.   if (Colors < 2L) return FALSE;
  80.  
  81.   VInfo = GetVisualInfo (Win->WScreen,TAG_END);
  82.  
  83.   if (! VInfo) return FALSE;
  84.  
  85.   COLOR_RGB [0L] = UNDO_RGB [0L] = Colors << 16L;
  86.  
  87.   COLOR_RGB [3L * Colors + 1L] = UNDO_RGB [3L * Colors + 1L] = NULL;
  88.  
  89.   COPY_RGB [0L] = COPY_RGB [1L] = COPY_RGB [2L] = NULL;
  90.  
  91.   GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  92.  
  93.   GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,Colors,&UNDO_RGB [1L]);
  94.  
  95.   RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  96.  
  97.   GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  98.  
  99.   BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  100.  
  101.   MyButtonGad = CreateContext (&GadList);
  102.  
  103.   BUTTON_GAD.ng_VisualInfo = PALETTE_GAD.ng_VisualInfo = SLIDER_GAD.ng_VisualInfo = VInfo;
  104.  
  105.   BUTTON_GAD.ng_LeftEdge   = 266;
  106.  
  107.   BUTTON_GAD.ng_TopEdge    = 10;
  108.  
  109.   BUTTON_GAD.ng_Width      = 90;
  110.  
  111.   BUTTON_GAD.ng_Height     = (Win->WScreen->Font->ta_YSize * 3) >> 1;
  112.  
  113.   BUTTON_GAD.ng_GadgetText = "C_opy";
  114.  
  115.   BUTTON_GAD.ng_GadgetID   = COPY;
  116.  
  117.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  118.  
  119.   BUTTON_GAD.ng_TopEdge   += (10 + BUTTON_GAD.ng_Height);
  120.  
  121.   BUTTON_GAD.ng_GadgetText = "S_wap";
  122.  
  123.   BUTTON_GAD.ng_GadgetID   = SWAP;
  124.  
  125.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  126.  
  127.   BUTTON_GAD.ng_TopEdge   += (10 + BUTTON_GAD.ng_Height);
  128.  
  129.   BUTTON_GAD.ng_GadgetText = "_Spread";
  130.  
  131.   BUTTON_GAD.ng_GadgetID   = SPREAD;
  132.  
  133.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  134.  
  135.   BUTTON_GAD.ng_TopEdge   += (10 + BUTTON_GAD.ng_Height);
  136.  
  137.   BUTTON_GAD.ng_GadgetText = "_Invert";
  138.  
  139.   BUTTON_GAD.ng_GadgetID   = INVERT;
  140.  
  141.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  142.  
  143.   BUTTON_GAD.ng_TopEdge   += (10 + BUTTON_GAD.ng_Height);
  144.  
  145.   BUTTON_GAD.ng_GadgetText = "_Undo";
  146.  
  147.   BUTTON_GAD.ng_GadgetID   = UNDO;
  148.  
  149.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  150.  
  151.   PALETTE_GAD.ng_LeftEdge   = 10;
  152.  
  153.   PALETTE_GAD.ng_TopEdge    = 10;
  154.  
  155.   PALETTE_GAD.ng_Width      = 250;
  156.  
  157.   PALETTE_GAD.ng_Height     = MAX (200,BUTTON_GAD.ng_TopEdge + BUTTON_GAD.ng_Height);
  158.  
  159.   PALETTE_GAD.ng_GadgetID   = PALETTE;
  160.  
  161.   MyPaletteGad = CreateGadget (PALETTE_KIND,MyButtonGad,&PALETTE_GAD,GTPA_Color,4,GTPA_IndicatorWidth,10,GTPA_NumColors,(UWORD) Colors,TAG_END);
  162.  
  163.   SLIDER_GAD.ng_LeftEdge    = 25;
  164.  
  165.   SLIDER_GAD.ng_TopEdge     = MAX (PALETTE_GAD.ng_TopEdge + PALETTE_GAD.ng_Height,BUTTON_GAD.ng_TopEdge + BUTTON_GAD.ng_Height) + 10;
  166.  
  167.   SLIDER_GAD.ng_Width       = 290;
  168.  
  169.   SLIDER_GAD.ng_Height      = Win->WScreen->Font->ta_YSize;
  170.  
  171.   SLIDER_GAD.ng_GadgetText  = "R";
  172.  
  173.   SLIDER_GAD.ng_GadgetID    = RED;
  174.  
  175.   RedSliderGad = CreateGadget (SLIDER_KIND,MyPaletteGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) RedLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,4,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  176.  
  177.   SLIDER_GAD.ng_TopEdge    += (8 + SLIDER_GAD.ng_Height);
  178.  
  179.   SLIDER_GAD.ng_GadgetText  = "G";
  180.  
  181.   SLIDER_GAD.ng_GadgetID    = GREEN;
  182.  
  183.   GreenSliderGad = CreateGadget (SLIDER_KIND,RedSliderGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) GreenLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,4,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  184.  
  185.   SLIDER_GAD.ng_TopEdge    += (8 + SLIDER_GAD.ng_Height);
  186.  
  187.   SLIDER_GAD.ng_GadgetText  = "B";
  188.  
  189.   SLIDER_GAD.ng_GadgetID    = BLUE;
  190.  
  191.   BlueSliderGad = CreateGadget (SLIDER_KIND,GreenSliderGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) BlueLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,4,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  192.  
  193.   BUTTON_GAD.ng_LeftEdge   = 10;
  194.  
  195.   BUTTON_GAD.ng_TopEdge    =  SLIDER_GAD.ng_TopEdge + SLIDER_GAD.ng_Height + 20;
  196.  
  197.   BUTTON_GAD.ng_GadgetText = "_Accept";
  198.  
  199.   BUTTON_GAD.ng_GadgetID   = ACCEPT;
  200.  
  201.   MyButtonGad = CreateGadget (BUTTON_KIND,BlueSliderGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  202.  
  203.   BUTTON_GAD.ng_LeftEdge  += (38 + BUTTON_GAD.ng_Width);
  204.  
  205.   BUTTON_GAD.ng_GadgetText = "Reset";
  206.  
  207.   BUTTON_GAD.ng_GadgetID   = RESET;
  208.  
  209.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,TAG_END);
  210.  
  211.   BUTTON_GAD.ng_LeftEdge  += (38 + BUTTON_GAD.ng_Width);
  212.  
  213.   BUTTON_GAD.ng_GadgetText = "_Cancel";
  214.  
  215.   BUTTON_GAD.ng_GadgetID   = CANCEL;
  216.  
  217.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  218.  
  219.   if (! MyButtonGad)
  220.   {
  221.      FreeGadgets (GadList);
  222.  
  223.      FreeVisualInfo (VInfo);
  224.  
  225.      return FALSE;
  226.   }
  227.  
  228.   ColorWin = OpenWindowTags (0,WA_Left,LeftEdge,
  229.                                WA_Top,TopEdge,
  230.                                WA_Width,BUTTON_GAD.ng_LeftEdge + BUTTON_GAD.ng_Width + 25,
  231.                                WA_Height,BUTTON_GAD.ng_TopEdge + BUTTON_GAD.ng_Height + 35,
  232.                                WA_Title,"Palette requester",
  233.                                WA_ScreenTitle,"Modify palette...",
  234.                                WA_CustomScreen,Win->WScreen,
  235.                                WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|IDCMP_VANILLAKEY|IDCMP_GADGETDOWN|BUTTONIDCMP|SLIDERIDCMP|PALETTEIDCMP,
  236.                                WA_Flags,WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_SIMPLE_REFRESH|WFLG_RMBTRAP|WFLG_GIMMEZEROZERO,
  237.                                WA_Gadgets,GadList,
  238.                                TAG_END);
  239.  
  240.   if (! ColorWin)
  241.   {
  242.      FreeGadgets (GadList);
  243.  
  244.      FreeVisualInfo (VInfo);
  245.  
  246.      return FALSE;
  247.   }
  248.  
  249.   GT_RefreshWindow (ColorWin,NULL);
  250.  
  251.   do { WaitPort (ColorWin->UserPort);
  252.  
  253.        if (Message = (struct IntuiMessage *) GT_GetIMsg (ColorWin->UserPort))
  254.        {
  255.           MyGad   = (struct Gadget *) Message->IAddress;
  256.  
  257.           MyClass = Message->Class;
  258.  
  259.           MyCode  = Message->Code;
  260.  
  261.           GT_ReplyIMsg ((struct IntuiMessage *) Message);
  262.  
  263.           switch (MyClass)
  264.           {
  265.                  case IDCMP_REFRESHWINDOW : GT_BeginRefresh (ColorWin);
  266.  
  267.                                             GT_EndRefresh   (ColorWin,TRUE);
  268.  
  269.                                             break;
  270.  
  271.                  case IDCMP_VANILLAKEY    : switch (MyCode)
  272.                                             {
  273.                                                     case 'a' :
  274.                                                     case 'A' : GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&Palette32 [1L]);
  275.  
  276.                                                                Exit = TRUE;
  277.  
  278.                                                                break;
  279.  
  280.                                                     case 'c' :
  281.                                                     case 'C' : LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  282.  
  283.                                                                Exit = TRUE;
  284.  
  285.                                                                break;
  286.  
  287.                                                     case 'o' :
  288.                                                     case 'O' : Copy (ColorWin,SelectedPen);
  289.  
  290.                                                                Copy_Msg = TRUE;
  291.  
  292.                                                                Swap_Msg = FALSE;
  293.  
  294.                                                                Spread_Msg = FALSE;
  295.  
  296.                                                                break;
  297.  
  298.                                                     case 'w' :
  299.                                                     case 'W' : OldPen = SelectedPen;
  300.  
  301.                                                                Copy_Msg = FALSE;
  302.  
  303.                                                                Swap_Msg = TRUE;
  304.  
  305.                                                                Spread_Msg = FALSE;
  306.  
  307.                                                                break;
  308.  
  309.                                                     case 's' :
  310.                                                     case 'S' : OldPen = SelectedPen;
  311.  
  312.                                                                Copy_Msg = FALSE;
  313.  
  314.                                                                Swap_Msg = FALSE;
  315.  
  316.                                                                Spread_Msg = TRUE;
  317.  
  318.                                                                break;
  319.  
  320.                                                     case 'u' :
  321.                                                     case 'U' : LoadRGB32 (ViewPortAddress (ColorWin),UNDO_RGB);
  322.  
  323.                                                                GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  324.  
  325.                                                                RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  326.  
  327.                                                                GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  328.  
  329.                                                                BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  330.  
  331.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  332.  
  333.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  334.  
  335.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  336.  
  337.                                                                break;
  338.  
  339.                                                     case 'E' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  340.  
  341.                                                                if (RedLevel > MINVALUE)
  342.                                                                {
  343.                                                                   KeepPalette (Win);
  344.  
  345.                                                                   RedLevel = (RedLevel < (MINVALUE + DELTA)) ? MINVALUE : RedLevel - DELTA;
  346.  
  347.                                                                   GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  348.  
  349.                                                                   COLOR_RGB [ColorBase] = RedLevel << 24L;
  350.  
  351.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  352.                                                                }
  353.  
  354.                                                                break;
  355.  
  356.                                                     case 'e' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  357.  
  358.                                                                if (RedLevel > MINVALUE)
  359.                                                                {
  360.                                                                   KeepPalette (Win);
  361.  
  362.                                                                   RedLevel--;
  363.  
  364.                                                                   GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  365.  
  366.                                                                   COLOR_RGB [ColorBase] = RedLevel << 24L;
  367.  
  368.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  369.                                                                }
  370.  
  371.                                                                break;
  372.  
  373.                                                     case 'T' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  374.  
  375.                                                                if (RedLevel < MAXVALUE)
  376.                                                                {
  377.                                                                   KeepPalette (Win);
  378.  
  379.                                                                   RedLevel += DELTA;
  380.  
  381.                                                                   if (RedLevel > MAXVALUE) RedLevel = MAXVALUE;
  382.  
  383.                                                                   GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  384.  
  385.                                                                   COLOR_RGB [ColorBase] = RedLevel << 24L;
  386.  
  387.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  388.                                                                }
  389.  
  390.                                                                break;
  391.  
  392.                                                     case 't' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  393.  
  394.                                                                if (RedLevel < MAXVALUE)
  395.                                                                {
  396.                                                                   KeepPalette (Win);
  397.  
  398.                                                                   RedLevel++;
  399.  
  400.                                                                   GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  401.  
  402.                                                                   COLOR_RGB [ColorBase] = RedLevel << 24L;
  403.  
  404.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  405.                                                                }
  406.  
  407.                                                                break;
  408.  
  409.                                                     case 'F' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  410.  
  411.                                                                if (GreenLevel > MINVALUE)
  412.                                                                {
  413.                                                                   KeepPalette (Win);
  414.  
  415.                                                                   GreenLevel = (GreenLevel < (MINVALUE + DELTA)) ? MINVALUE : GreenLevel - DELTA;
  416.  
  417.                                                                   GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  418.  
  419.                                                                   COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  420.  
  421.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  422.                                                                }
  423.  
  424.                                                                break;
  425.  
  426.                                                     case 'f' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  427.  
  428.                                                                if (GreenLevel > MINVALUE)
  429.                                                                {
  430.                                                                   KeepPalette (Win);
  431.  
  432.                                                                   GreenLevel--;
  433.  
  434.                                                                   GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  435.  
  436.                                                                   COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  437.  
  438.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  439.                                                                }
  440.  
  441.                                                                break;
  442.  
  443.                                                     case 'H' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  444.  
  445.                                                                if (GreenLevel < MAXVALUE)
  446.                                                                {
  447.                                                                   KeepPalette (Win);
  448.  
  449.                                                                   GreenLevel += DELTA;
  450.  
  451.                                                                   if (GreenLevel > MAXVALUE) GreenLevel = MAXVALUE;
  452.  
  453.                                                                   GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  454.  
  455.                                                                   COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  456.  
  457.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  458.                                                                }
  459.  
  460.                                                                break;
  461.  
  462.                                                     case 'h' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  463.  
  464.                                                                if (GreenLevel < MAXVALUE)
  465.                                                                {
  466.                                                                   KeepPalette (Win);
  467.  
  468.                                                                   GreenLevel++;
  469.  
  470.                                                                   GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  471.  
  472.                                                                   COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  473.  
  474.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  475.                                                                }
  476.  
  477.                                                                break;
  478.  
  479.                                                     case 'V' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  480.  
  481.                                                                if (BlueLevel > MINVALUE)
  482.                                                                {
  483.                                                                   KeepPalette (Win);
  484.  
  485.                                                                   BlueLevel = (BlueLevel < (MINVALUE + DELTA)) ? MINVALUE : BlueLevel - DELTA;
  486.  
  487.                                                                   GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  488.  
  489.                                                                   COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  490.  
  491.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  492.                                                                }
  493.  
  494.                                                                break;
  495.  
  496.                                                     case 'v' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  497.  
  498.                                                                if (BlueLevel > MINVALUE)
  499.                                                                {
  500.                                                                   KeepPalette (Win);
  501.  
  502.                                                                   BlueLevel--;
  503.  
  504.                                                                   GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  505.  
  506.                                                                   COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  507.  
  508.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  509.                                                                }
  510.  
  511.                                                                break;
  512.  
  513.                                                     case 'N' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  514.  
  515.                                                                if (BlueLevel < MAXVALUE)
  516.                                                                {
  517.                                                                   KeepPalette (Win);
  518.  
  519.                                                                   BlueLevel += DELTA;
  520.  
  521.                                                                   if (BlueLevel > MAXVALUE) BlueLevel = MAXVALUE;
  522.  
  523.                                                                   GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  524.  
  525.                                                                   COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  526.  
  527.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  528.                                                                }
  529.  
  530.                                                                break;
  531.  
  532.                                                     case 'n' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  533.  
  534.                                                                if (BlueLevel < MAXVALUE)
  535.                                                                {
  536.                                                                   KeepPalette (Win);
  537.  
  538.                                                                   BlueLevel++;
  539.  
  540.                                                                   GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  541.  
  542.                                                                   COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  543.  
  544.                                                                   LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  545.                                                                }
  546.  
  547.                                                                break;
  548.  
  549.                                                     case 'I' :
  550.                                                     case 'i' : KeepPalette (Win);
  551.  
  552.                                                                InvertPalette (Win,STARTPEN,Colors - 1L);
  553.  
  554.                                                                RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  555.  
  556.                                                                GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  557.  
  558.                                                                BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  559.  
  560.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  561.  
  562.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  563.  
  564.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  565.  
  566.                                                                break;
  567.                                             }
  568.  
  569.                                             break;
  570.  
  571.                  case IDCMP_GADGETUP      : switch (MyGad->GadgetID)
  572.                                             {
  573.                                                     case ACCEPT  : GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&Palette32 [1L]);
  574.  
  575.                                                                    Exit = TRUE;
  576.  
  577.                                                                    break;
  578.  
  579.                                                     case RESET   : KeepPalette (Win);
  580.  
  581.                                                                    LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  582.  
  583.                                                                    GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  584.  
  585.                                                                    RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  586.  
  587.                                                                    GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  588.  
  589.                                                                    BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  590.  
  591.                                                                    GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  592.  
  593.                                                                    GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  594.  
  595.                                                                    GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  596.  
  597.                                                                    break;
  598.  
  599.                                                     case CANCEL  : LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  600.  
  601.                                                                    Exit = TRUE;
  602.  
  603.                                                                    break;
  604.  
  605.                                                     case COPY    : Copy (ColorWin,SelectedPen);
  606.  
  607.                                                                    Copy_Msg = TRUE;
  608.  
  609.                                                                    Swap_Msg = FALSE;
  610.  
  611.                                                                    Spread_Msg = FALSE;
  612.  
  613.                                                                    break;
  614.  
  615.                                                     case SWAP    : OldPen = SelectedPen;
  616.  
  617.                                                                    Copy_Msg = FALSE;
  618.  
  619.                                                                    Swap_Msg = TRUE;
  620.  
  621.                                                                    Spread_Msg = FALSE;
  622.  
  623.                                                                    break;
  624.  
  625.                                                     case SPREAD  : OldPen = SelectedPen;
  626.  
  627.                                                                    Copy_Msg = FALSE;
  628.  
  629.                                                                    Swap_Msg = FALSE;
  630.  
  631.                                                                    Spread_Msg = TRUE;
  632.  
  633.                                                                    break;
  634.  
  635.                                                     case INVERT  : KeepPalette (Win);
  636.  
  637.                                                                    InvertPalette (Win,STARTPEN,Colors - 1L);
  638.  
  639.                                                                    RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  640.  
  641.                                                                    GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  642.  
  643.                                                                    BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  644.  
  645.                                                                    GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  646.  
  647.                                                                    GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  648.  
  649.                                                                    GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  650.  
  651.                                                                    break;
  652.  
  653.                                                     case UNDO    : LoadRGB32 (ViewPortAddress (ColorWin),UNDO_RGB);
  654.  
  655.                                                                    GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  656.  
  657.                                                                    RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  658.  
  659.                                                                    GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  660.  
  661.                                                                    BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  662.  
  663.                                                                    GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  664.  
  665.                                                                    GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  666.  
  667.                                                                    GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  668.  
  669.                                                                    break;
  670.  
  671.                                                     case RED     : KeepPalette (Win);
  672.  
  673.                                                                    GT_GetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,&RedLevel);
  674.  
  675.                                                                    COLOR_RGB [ColorBase]      = RedLevel << 24L;
  676.  
  677.                                                                    LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  678.  
  679.                                                                    break;
  680.  
  681.                                                     case GREEN   : KeepPalette (Win);
  682.  
  683.                                                                    GT_GetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,&GreenLevel);
  684.  
  685.                                                                    COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  686.  
  687.                                                                    LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  688.  
  689.                                                                    break;
  690.  
  691.                                                     case BLUE    : KeepPalette (Win);
  692.  
  693.                                                                    GT_GetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,&BlueLevel);
  694.  
  695.                                                                    COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  696.  
  697.                                                                    LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  698.  
  699.                                                                    break;
  700.  
  701.                                                     case PALETTE : GT_GetGadgetAttrs (MyPaletteGad,ColorWin,NULL,GTPA_Color,&SelectedPen);
  702.  
  703.                                                                    if (Copy_Msg)
  704.                                                                    {
  705.                                                                       KeepPalette (Win);
  706.  
  707.                                                                       Paste (ColorWin,SelectedPen);
  708.  
  709.                                                                       Copy_Msg = FALSE;
  710.                                                                    }
  711.  
  712.                                                                    if (Swap_Msg)
  713.                                                                    {
  714.                                                                       KeepPalette (Win);
  715.  
  716.                                                                       NewPen = SelectedPen;
  717.  
  718.                                                                       Swap (ColorWin,OldPen,NewPen);
  719.  
  720.                                                                       Swap_Msg = FALSE;
  721.                                                                    }
  722.  
  723.                                                                    if (Spread_Msg)
  724.                                                                    {
  725.                                                                       KeepPalette (Win);
  726.  
  727.                                                                       NewPen = SelectedPen;
  728.  
  729.                                                                       Spread (ColorWin,OldPen,NewPen);
  730.  
  731.                                                                       Spread_Msg = FALSE;
  732.                                                                    }
  733.  
  734.                                                                    ColorBase = 3L * SelectedPen + 1L;
  735.  
  736.                                                                    RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  737.  
  738.                                                                    GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  739.  
  740.                                                                    BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  741.  
  742.                                                                    GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  743.  
  744.                                                                    GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  745.  
  746.                                                                    GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  747.  
  748.                                                                    break;
  749.                                             }
  750.  
  751.                                             break;
  752.  
  753.                  case IDCMP_CLOSEWINDOW   : Exit = TRUE;
  754.  
  755.                                             break;
  756.           }
  757.        }
  758.  
  759.      } while (Exit == FALSE);
  760.  
  761.   CloseWindow (ColorWin);
  762.  
  763.   FreeGadgets (GadList);
  764.  
  765.   FreeVisualInfo (VInfo);
  766.  
  767.   return TRUE;
  768. }
  769.  
  770. VOID Copy (struct Window *Win,ULONG PenNumber)
  771. {
  772.   GetRGB32 (ViewPortAddress (Win)->ColorMap,PenNumber,1L,COPY_RGB);
  773. }
  774.  
  775. VOID Paste (struct Window *Win,const ULONG PenNumber)
  776. {
  777. const ULONG ColorBase = 3L * PenNumber + 1L;
  778.  
  779. ULONG Index;
  780.  
  781.   for (Index = 0L; Index < 3L; Index++) COLOR_RGB [ColorBase + Index] = COPY_RGB [Index];
  782.  
  783.   LoadRGB32 (ViewPortAddress (Win),COLOR_RGB);
  784. }
  785.  
  786. VOID Swap (struct Window *Win,const ULONG OldPen,const ULONG NewPen)
  787. {
  788. ULONG Tmp_RGB [3L];
  789.  
  790. ULONG Index;
  791.  
  792.    GetRGB32 (ViewPortAddress (Win)->ColorMap,OldPen,1L,Tmp_RGB);
  793.  
  794.    Copy (Win,NewPen);
  795.  
  796.    Paste (Win,OldPen);
  797.  
  798.    for (Index = 0L; Index < 3L; Index++) COPY_RGB [Index] = Tmp_RGB [Index];
  799.  
  800.    Paste (Win,NewPen);
  801. }
  802.  
  803. BOOL Spread (struct Window *Win,const ULONG OldPen,const ULONG NewPen)
  804. {
  805. const ULONG StartPen = MIN (OldPen,NewPen) , EndPen = MAX (OldPen,NewPen);
  806.  
  807. const ULONG  Range   = EndPen - StartPen;
  808.  
  809. LONG RedStep,GreenStep,BlueStep;
  810.  
  811. ULONG Index,RedLevel,GreenLevel,BlueLevel,ColorBase;
  812.  
  813.   if (Range < 2L) return TRUE;
  814.  
  815.   ColorBase   = 3L * StartPen + 1L;
  816.  
  817.   RedLevel    = COLOR_RGB [ColorBase]      >> 8L;
  818.  
  819.   GreenLevel  = COLOR_RGB [ColorBase + 1L] >> 8L;
  820.  
  821.   BlueLevel   = COLOR_RGB [ColorBase + 2L] >> 8L;
  822.  
  823.   ColorBase   = 3L * EndPen + 1L;
  824.  
  825.   RedStep     = (LONG) (COLOR_RGB [ColorBase]      >> 8L);
  826.  
  827.   GreenStep   = (LONG) (COLOR_RGB [ColorBase + 1L] >> 8L);
  828.  
  829.   BlueStep    = (LONG) (COLOR_RGB [ColorBase + 2L] >> 8L);
  830.  
  831.   RedStep    -= (LONG) RedLevel;
  832.  
  833.   GreenStep  -= (LONG) GreenLevel;
  834.  
  835.   BlueStep   -= (LONG) BlueLevel;
  836.  
  837.   RedStep    /= (LONG) Range;
  838.  
  839.   GreenStep  /= (LONG) Range;
  840.  
  841.   BlueStep   /= (LONG) Range;
  842.  
  843.   for (Index = (StartPen + 1L); Index < EndPen; Index++)
  844.   {
  845.       RedLevel   += RedStep;
  846.  
  847.       GreenLevel += GreenStep;
  848.  
  849.       BlueLevel  += BlueStep;
  850.  
  851.       ColorBase   = 3L * Index + 1L;
  852.  
  853.       COLOR_RGB [ColorBase]      = RedLevel   << 8L;
  854.  
  855.       COLOR_RGB [ColorBase + 1L] = GreenLevel << 8L;
  856.  
  857.       COLOR_RGB [ColorBase + 2L] = BlueLevel  << 8L;
  858.   }
  859.  
  860.   LoadRGB32 (ViewPortAddress (Win),COLOR_RGB);
  861.  
  862.   return FALSE;
  863. }
  864.  
  865. VOID KeepPalette (struct Window *Win)
  866. {
  867. ULONG Colors;
  868.  
  869.   Colors = 1L << (Win->RPort->BitMap->Depth);
  870.  
  871.   GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,Colors,&UNDO_RGB [1L]);
  872. }
  873.  
  874. VOID InvertPalette (struct Window *Win,ULONG StartPen,ULONG EndPen)
  875. {
  876.   while (StartPen < EndPen) Swap (Win,StartPen++,EndPen--);
  877. }
  878.